<?php
require_once("../../class2.php");
require_once(HEADERF);
require_once("./includes/require_all.php");

ramka("open");

if (isset($_POST['randme'])){

	$toclear= array(",", "!", "?", ".", "	", "  ");
	$toadd=   array(" ", " ", " ", " ", " ", " ");
	$quested= explode(" ", str_replace($toclear, $toadd, mb_strtolower($_POST['randme'],'UTF-8')));
	clear_array($quested);
	$toanswer= array();
	if (in_array('кога', $quested)) {
		$toanswer+= array (
			"Когато-тогава.",
			"Скоро.",
			"Ще ти се, нали?",
			"Никога.",
			"Някой ден.");
		}
	if (in_array('докога', $quested)) {
		$toanswer+= array (
			"Един бог знае.",
			"Ще са нужни 13 века, 15 години и един кит.",
			"Знам ли?");
		}
	if (in_array('защо', $quested)) {
		$toanswer+= array (
			"Мен ли питаш?",
			"Последният, който попита, беше ликвидиран от хакера.",
			"Иска ми се да знаех.");
		}
	if (in_array('колко', $quested)) {
		$toanswer+= array (
			"Колкото трябва.",
			"");
		}		
	if (in_array('ще', $quested) && $toanswer[0]==null) {
		$toanswer+= array (
			"Да.",
			"Може би.",
			"Не.");
		}			
	if ($toanswer[0]==null) {
		$toanswer+= array (
			"Да.",
			"Може би.",
			"Попитай пак.",
			"Не.");
		}
		shuffle($toanswer);
		clear_array($toanswer);

	$beenasked= mysql_real_escape_string($_POST['randme']);
	$beenanswered= array_shift($toanswer);
	$uniqued= mysql_fetch_array(mysql_query("SELECT `unique` FROM cms_magicball WHERE userip='".USERIP."' LIMIT 0,1"));
	if ($uniqued['unique']==null) {
		$unitemp=mysql_fetch_array(mysql_query("SELECT MAX( `unique` ) AS uniquer FROM cms_magicball"));
		$uniqued['unique']= $unitemp['uniquer']+1;
	}
	
	if ($quested[0]!='') {
		mysql_query("INSERT INTO cms_magicball(`unique`, `username`, `userip`, `question`, `answer`) VALUES ('$uniqued[unique]', '".USERNAME."', '".USERIP."', '$beenasked', '$beenanswered')");
		echo "<center class='forumheader3'><div class='textYELLOW'>$beenasked</div> <div class='textGREEN'>- $beenanswered</div></center>";
	}
	else echo "<center class='forumheader3 textRED'>Какво питаше?</center>";
	

}

echo "<div class='forumheader3'>
		<form name='input' action='magicball.php' method='post'>
		<span class='textWHITE'> Питам: </span> <input type='text' name='randme' class='forumheader2 textYELLOW' style='width:75%;'/>
		<input type='submit' value='Попитай' class='button'/>
		</form> 
	  </div>";
	  
$allstupidques= mysql_query("SELECT * FROM cms_magicball ORDER BY date DESC");
echo "<div class='forumheader3'>
		<table class='forumheader2' WIDTH=100%><tr class='forumheader'>
			<td></td>
			<td>Гост:</td>
			<td>попита:</td>
			<td>и му беше отговорено:</td>
			<td>в:</td>
		</tr>";
$numbered=1;
while ($stq= mysql_fetch_array($allstupidques)) {
	$stq['date']= months_inBG(date("j F : H:i", strtotime($stq['date'])));
	echo "<tr class='forumheader3'>
			<td>$numbered</td>
			<td>$stq[unique]</td>
			<td class='textYELLOW'>$stq[question]</td>
			<td class='textGREEN'>$stq[answer]</td>
			<td>$stq[date]</td>
		 </tr>";
		 $numbered++;
}
	echo "</table></div>";

ramka("close");
require_once(FOOTERF);
?>